package user_assets

import (
	"context"

	xerrors "gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserBankListPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 分页查询
func NewUserBankListPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserBankListPageLogic {
	return &UserBankListPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserBankListPageLogic) UserBankListPage(req *types.UserBankInfoPageRequest) (resp *types.UserBankInfoPageResponse, err error) {
	request := rpc.UserBankInfoPageRequest{}
	err = copier.Copy(&request, req)
	request.Param.CardNumber = req.Param.CardNum
	if err != nil {
		return nil, xerrors.New(err)
	}
	page, err := l.svcCtx.AssetsRpc.UserChannel.UserBankInfoPage(l.ctx, &request)
	var infos []types.UserBankInfo
	if err != nil {
		return &types.UserBankInfoPageResponse{
			DataList: infos,
			Total:    0,
		}, err
	}
	if len(page.DataList) == 0 {
		return &types.UserBankInfoPageResponse{
			DataList: infos,
			Total:    0,
		}, err
	}
	for _, bankInfo := range page.DataList {
		infos = append(infos, types.UserBankInfo{
			Id:         bankInfo.Id,
			UserId:     bankInfo.UserId,
			UserName:   bankInfo.UserName,
			CardNum:    bankInfo.CardNumber,
			Email:      bankInfo.Email,
			Phone:      bankInfo.Phone,
			BankName:   bankInfo.BankName,
			Province:   bankInfo.Province,
			City:       bankInfo.City,
			IFSCCode:   bankInfo.IfscCode,
			BranchBank: bankInfo.BranchBank,
			UpdatedAt:  bankInfo.UpdatedAt,
			BankId:     bankInfo.BankId,
		})
	}
	return &types.UserBankInfoPageResponse{
		DataList: infos,
		Total:    page.Total,
	}, nil
}
